package com.zenika.zenbinding.client.renderer;

import java.util.List;

import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.zenika.zenbinding.client.ZenBinder;
import com.zenika.zenbinding.client.feedback.DummyFeedbackManager;
import com.zenika.zenbinding.client.feedback.FeedbackManager;
import com.zenika.zenbinding.client.handler.ListBindingHandler;
import com.zenika.zenbinding.client.model.BusinessObject;
import com.zenika.zenbinding.client.model.ListModel;

public class ListBoxRenderer implements Renderer {

	private String property;
	private BusinessObject[] choices;
	private String choicesProperty;
	private String style;
	private FeedbackManager feedback;

	public ListBoxRenderer(String property, BusinessObject[] choices, String choicesProperty) {
		this.property = property;
		this.choices = choices;
		this.choicesProperty = choicesProperty;
	}

	public ListBoxRenderer(String property, List choices, String choicesProperty) {
		this.property = property;
		this.choicesProperty = choicesProperty;
		Object[] objects = choices.toArray();
		this.choices = new BusinessObject[objects.length];
		for (int i = 0; i < objects.length; i++) {
			this.choices[i] = (BusinessObject) objects[i];
		}
	}

	public Widget render(BusinessObject object) {
		ListBox listBox = new ListBox();
		if (this.style != null) {
			listBox.setStyleName(this.style);
		}
		ListBindingHandler handler;
		handler = ZenBinder.createListBindingHandler(listBox, this.choices, this.choicesProperty);
		ZenBinder.bindList(handler, object, this.property);
		return listBox;
	}

	public Widget render(BusinessObject object, DummyFeedbackManager manager) {
		ListBox listBox = new ListBox();
		if (this.style != null) {
			listBox.setStyleName(this.style);
		}
		ListBindingHandler handler;
		handler = ZenBinder.createListBindingHandler(listBox, this.choices, this.choicesProperty);
		ListModel model = ZenBinder.bindList(handler, object, this.property);
		if (this.feedback != null) {
			manager.setFeedbackManager(this.feedback);
		}
		model.setFeedbackManager(manager);
		return listBox;
	}

	public void setStyleName(String style) {
		this.style = style;
	}

	public void setFeedbackManager(FeedbackManager feedback) {
		this.feedback = feedback;

	}

}
